package com.polaris.live.im.utils

import com.polaris.live.common.util.JsonUtils
import com.polaris.live.im.sfs.BIG_BROTHER_ENTERS_ROOM
import com.polaris.live.im.sfs.CALL_GAME_ANSWER
import com.polaris.live.im.sfs.CALL_GAME_STATUS
import com.polaris.live.im.sfs.CALL_LOAD_GAME
import com.polaris.live.im.sfs.CALL_OTHER_CAMERA
import com.polaris.live.im.sfs.CALL_REFUSE_INTERPRETATION
import com.polaris.live.im.sfs.LIVE_CHANGED_GAME_REWARD
import com.polaris.live.im.sfs.LIVE_GIFT_LIST
import com.polaris.live.im.sfs.LIVE_INVITE_CALL
import com.polaris.live.im.sfs.LIVE_INVITE_FOLLOW
import com.polaris.live.im.sfs.LIVE_INVITE_GIFT
import com.polaris.live.im.sfs.LIVE_INVITE_SITE
import com.polaris.live.im.sfs.LIVE_MAGIC_LAMP_OPEN
import com.polaris.live.im.sfs.LIVE_MASKING_MESSAGE
import com.polaris.live.im.sfs.LIVE_NEW_ANCHOR
import com.polaris.live.im.sfs.LIVE_NEW_FOLLOWER
import com.polaris.live.im.sfs.LIVE_NOTICE_MESSAGE
import com.polaris.live.im.sfs.LIVE_OCCLUSION_MESSAGE
import com.polaris.live.im.sfs.LIVE_PARTY_EMOJI
import com.polaris.live.im.sfs.LIVE_PROMPT
import com.polaris.live.im.sfs.LIVE_PUBLIC_SCREEN
import com.polaris.live.im.sfs.LIVE_RECHARGE_PAY
import com.polaris.live.im.sfs.LIVE_RECOMMEND_CARD
import com.polaris.live.im.sfs.LIVE_RELOAD_GAME
import com.polaris.live.im.sfs.LIVE_SEND_GIFT
import com.polaris.live.im.sfs.LIVE_TOAST_MESSAGE
import com.polaris.live.im.sfs.LIVE_TRANSLATION_MESSAGE
import com.polaris.live.im.sfs.LIVE_UPLOAD_FACE
import com.polaris.live.im.sfs.LIVE_UP_SEAT_INVITE
import com.polaris.live.im.sfs.LIVE_WARN_MESSAGE
import com.polaris.live.im.sfs.LUCKY_BACKPACK_GIFT
import com.polaris.live.im.sfs.REQUEST_GIFT_POPUP_MESSAGE
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.MessageUserExtBean
import com.polaris.live.im.sfs.bean.live.payload.CallGameStatusPayloadDto
import com.polaris.live.im.sfs.bean.live.payload.CallLaunchRechargePayloadBean
import com.polaris.live.im.sfs.bean.live.payload.CallMaskPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.CallObstructPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.CallStreamSwitchPayloadDto
import com.polaris.live.im.sfs.bean.live.payload.ContributionBarragePayloadDto
import com.polaris.live.im.sfs.bean.live.payload.GiftListPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.InviteCallPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.InviteFollowPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.InviteGiftPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.InviteGiftPopupPayloadDto
import com.polaris.live.im.sfs.bean.live.payload.InviteSiteNoticeExtBean
import com.polaris.live.im.sfs.bean.live.payload.InviteSiteNoticePayloadBean
import com.polaris.live.im.sfs.bean.live.payload.LiveWarningPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.NewAnchorSubsidyPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.NoticePayloadBean
import com.polaris.live.im.sfs.bean.live.payload.PartyEmojiPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.PublicScreenPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.SimultaneousStartPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.TipsPayloadBean
import com.polaris.live.im.sfs.bean.live.payload.ToastPayloadBean
import com.polaris.live.resp.bean.MagicLampCountDownBean
import com.smartfoxserver.v2.entities.data.ISFSObject

/**
 * 通话消息解析
 *
 * @author Created by 半仙 on 2023/9/13/013 15:44
 */
/**
 * 通话公屏消息
 */
fun sfsObjectLiveMessageRequest(sfsObject: ISFSObject): SendLiveMessage {
    val msgId = sfsObject.getUtfString("msgId")

    val businessType = sfsObject.getInt("businessType")
    val businessId = sfsObject.getLong("businessId")
    val type = sfsObject.getInt("type")
    val content = sfsObject.getUtfString("content")
    val tempPayload = sfsObject.getUtfString("payload")
    val tempExt = sfsObject.getUtfString("ext")


    val (payload, ext) = when (type?.toInt()) {
        LIVE_PUBLIC_SCREEN -> {//公屏消息
            JsonUtils.fromJson<PublicScreenPayloadBean>(tempPayload) to
                    JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_PROMPT -> {    //提示消息
            JsonUtils.fromJson<TipsPayloadBean>(tempPayload) to
                    null
        }

        LIVE_SEND_GIFT, LIVE_INVITE_GIFT, LUCKY_BACKPACK_GIFT -> {//通话礼物消息
            JsonUtils.fromJson<InviteGiftPayloadBean>(tempPayload) to
                    JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_INVITE_FOLLOW -> {//通话邀请关注消息
            JsonUtils.fromJson<InviteFollowPayloadBean>(tempPayload) to
                    JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_GIFT_LIST -> {//礼物列表消息
            JsonUtils.fromJson<GiftListPayloadBean>(tempPayload) to null
        }
        //这个是ios用的 Android不需要接
//        LIVE_SIMULTANEOUS_SWITCH -> {//通话同声传译开关
//            JsonUtils.fromJson<SimultaneousSwitchPayloadBean>(tempPayload) to
//                    JsonUtils.fromJson<MessageUserExtBean>(tempExt)
//        }

        LIVE_INVITE_CALL -> {//邀请通话
            JsonUtils.fromJson<InviteCallPayloadBean>(tempPayload) to
                    JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_RECHARGE_PAY -> {//拉起充值页面
            JsonUtils.fromJson<CallLaunchRechargePayloadBean>(tempPayload) to null
        }

        LIVE_OCCLUSION_MESSAGE -> {//遮挡屏幕消息
            JsonUtils.fromJson<CallObstructPayloadBean>(tempPayload) to null
        }

        LIVE_NOTICE_MESSAGE -> {//通知消息
            JsonUtils.fromJson<NoticePayloadBean>(tempPayload) to null
        }

        LIVE_TOAST_MESSAGE -> {//toast消息提示
            JsonUtils.fromJson<ToastPayloadBean>(tempPayload) to null
        }

        LIVE_WARN_MESSAGE -> {//警告消息
            JsonUtils.fromJson<LiveWarningPayloadBean>(tempPayload) to null
        }

        LIVE_MASKING_MESSAGE -> {//遮罩消息
            JsonUtils.fromJson<CallMaskPayloadBean>(tempPayload) to null
        }

        LIVE_TRANSLATION_MESSAGE -> {//开启同声传译消息
            JsonUtils.fromJson<SimultaneousStartPayloadBean>(tempPayload) to null
        }

        LIVE_UPLOAD_FACE -> {//上传人脸消息
            null to null
        }

        LIVE_NEW_FOLLOWER -> {//关注消息
            null to JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_NEW_ANCHOR -> {//新主播补贴消息
            JsonUtils.fromJson<NewAnchorSubsidyPayloadBean>(tempPayload) to null
        }

        LIVE_INVITE_SITE -> {//邀请上座信息
            JsonUtils.fromJson<InviteSiteNoticePayloadBean>(tempPayload) to
                    JsonUtils.fromJson<InviteSiteNoticeExtBean>(tempExt)
        }

        LIVE_UP_SEAT_INVITE -> {//上座消息
            null to JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_PARTY_EMOJI -> {//表情消息
            JsonUtils.fromJson<PartyEmojiPayloadBean>(tempPayload) to null
        }

        LIVE_CHANGED_GAME_REWARD -> {//游戏价格修改
            null to null
        }

        LIVE_RELOAD_GAME -> { //重新加载游戏
            null to null
        }

        CALL_GAME_STATUS -> { //通话游戏状态
            JsonUtils.fromJson<CallGameStatusPayloadDto>(tempPayload) to null
        }

        CALL_GAME_ANSWER -> { //通话游戏开始
            null to null
        }

        CALL_OTHER_CAMERA -> { //对方控制了摄像头
            JsonUtils.fromJson<CallStreamSwitchPayloadDto>(tempPayload) to null
        }

        CALL_LOAD_GAME -> { //游戏加载消息
            null to null
        }

        CALL_REFUSE_INTERPRETATION -> { //拒绝同声传译
            null to null
        }

        REQUEST_GIFT_POPUP_MESSAGE -> { //索要礼物弹出消息
            JsonUtils.fromJson<InviteGiftPopupPayloadDto>(tempPayload) to null
        }

        BIG_BROTHER_ENTERS_ROOM -> { //大哥进场
            JsonUtils.fromJson<ContributionBarragePayloadDto>(tempPayload) to JsonUtils.fromJson<MessageUserExtBean>(
                tempExt
            )
        }

        LIVE_RECOMMEND_CARD -> { // 使用推荐卡片
            null to JsonUtils.fromJson<MessageUserExtBean>(tempExt)
        }

        LIVE_MAGIC_LAMP_OPEN -> { // 神灯开奖
            JsonUtils.fromJson<MagicLampCountDownBean>(tempPayload) to null
        }

        else -> null to null
    }

    val timestamp = sfsObject.getLong("timestamp")

    return SendLiveMessage(
        msgId = msgId,
        businessType = businessType,
        businessId = businessId,
        type = type,
        content = content,
        payload = payload,
        ext = ext,
        timestamp = timestamp
    )
}
